package com.cooldatasoft.domain.serviceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooldatasoft.domain.dao.DocumentDao;
import com.cooldatasoft.domain.dao.ProjectsDao;
import com.cooldatasoft.domain.dao.TaskDao;
import com.cooldatasoft.domain.dao.UserProjectRolesDao;
import com.cooldatasoft.domain.model.Document;
import com.cooldatasoft.domain.model.Project;
import com.cooldatasoft.domain.model.Task;
import com.cooldatasoft.domain.model.UserProjectRoles;
import com.cooldatasoft.domain.service.ProjectsService;


@Service("projectsService")
public class ProjectsServiceImpl implements ProjectsService {
	
	@Autowired
	private ProjectsDao projectsDao;
	
	@Autowired
	private UserProjectRolesDao userProjectsRolesDao;
	
	@Autowired
	private TaskDao taskDao;
	
	@Autowired
	private DocumentDao documentDao;

	public Long insertProjects(Project projects) {
		return projectsDao.insert(projects);
	}

	public void updateProjects(Project projects) {
		projectsDao.update(projects);		
	}

	public void deleteProjects(Project projects) {
		List<UserProjectRoles> relations = userProjectsRolesDao.getUsersOnProject(projects);
		if (relations != null) {
			for (UserProjectRoles relation : relations) {
				userProjectsRolesDao.delete(relation.getIdThis());
			}
		}
		
		List<Task> tasks = taskDao.getTasksToProject(projects);
		if (tasks != null) {
			for (Task task : tasks) {
				taskDao.delete(task.getIdTask());
			}
		}
		
		List<Document> documents = documentDao.getByProject(projects);
		for (Document doc : documents) {
			documentDao.delete(doc.getId());
		}
		
		projectsDao.delete(projects.getIdProject());	
	}

	public List<Project> getAllProjects() {
		return projectsDao.getAll();
	}

	public Project getProjectsByIdProject(Long id){
		return projectsDao.getById(id);
	}
	

}
